package com.nativetalk.service.three.giving.search;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.giving.TdNtLiveGivingHistoryMemberList;
import com.nativetalk.bean.giving.TdNtLiveGivingHistoryTeacherList;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.teacher.TdNtTeacherIndex;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtLiveGivingHistoryDao;
import com.nativetalk.dao.TdNtMemberDao;
import com.nativetalk.dao.TdNtTeacherDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 16/6/24.
 */
@Service
public class ThreeLiveGivingSearchServiceImpl implements ThreeLiveGivingSearchService{
    private static Logger logger = Logger.getLogger(ThreeLiveGivingSearchServiceImpl.class);
    @Resource
    private TdNtLiveGivingHistoryDao tdNtLiveGivingHistoryDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Override
    public RetInfo findGivingByMember(Page page) {
        String logInfo = this.getClass().getName() + ":findGivingByMember:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveGivingHistoryDao.findTdNtLiveGivingHistoryByMembIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveGivingHistoryMemberList> tdNtLiveGivingHistoryMemberLists = tdNtLiveGivingHistoryDao.findTdNtLiveGivingHistoryByMembIdPage(page);
                for (int i=0 ; i < tdNtLiveGivingHistoryMemberLists.size() ; i ++ ) {
                    TdNtTeacherIndex tdNtTeacherIndex = tdNtTeacherDao.findTdNtTeacherIndex(tdNtLiveGivingHistoryMemberLists.get(i).getTea_id());
                    tdNtLiveGivingHistoryMemberLists.get(i).setTea_head_portrait(tdNtTeacherIndex.getTea_head_portrait());
                    tdNtLiveGivingHistoryMemberLists.get(i).setTea_name(tdNtTeacherIndex.getTea_name());
                }
                result.put("givings", tdNtLiveGivingHistoryMemberLists);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findGivingByTeacher(Page page) {
        String logInfo = this.getClass().getName() + ":findGivingByTeacher:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String, Object> result = new HashMap<>();
            int currentPage = page.getCurrentPage();
            int pageSize = page.getPageSize();
            int totalPage;//总页数
            int totalRecord = tdNtLiveGivingHistoryDao.findTdNtLiveGivingHistoryByTeaIdSize(page);//总条数
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord/pageSize;
                } else {
                    totalPage = (totalRecord/pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    page.setCurrentPage(1);
                }
                page.setCurrentPage((page.getCurrentPage()-1)*pageSize);

                List<TdNtLiveGivingHistoryTeacherList> tdNtLiveGivingHistoryTeacherLists = tdNtLiveGivingHistoryDao.findTdNtLiveGivingHistoryByTeaIdPage(page);
                for (int i=0 ; i < tdNtLiveGivingHistoryTeacherLists.size() ; i ++ ) {
                    TdNtMemberDetail tdNtMemberDetail = tdNtMemberDao.findTdNtMemberDetail(tdNtLiveGivingHistoryTeacherLists.get(i).getMemb_id());
                    tdNtLiveGivingHistoryTeacherLists.get(i).setMemb_name(tdNtMemberDetail.getMemb_name());
                    tdNtLiveGivingHistoryTeacherLists.get(i).setMemb_head_portrait(tdNtMemberDetail.getMemb_head_portrait());
                }
                result.put("givings", tdNtLiveGivingHistoryTeacherLists);
                result.put("totalPage", totalPage);

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("获取直播成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip("还未添加直播");
            }
            retInfo.setObj(result);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
